/*
 * File:    XPathParser.java
 * Created: 29-Jun-2007
 * 
 * by Kenji Hollis <kenji@bitgatesoftware.com>
 * Released under the Creative Commons License version 2.5
 */

package com.bitgate.util.node;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Node;

/**
 * This class serves as a utility class to take a node from an XML file, and apply an XPath expression on it.
 * Once the XPath expression has been used, the return value is a <code>String</code>.  This class may grow to contain
 * more XPathParsing functionality.
 * 
 * @author Kenji Hollis &lt;kenji@bitgatesoftware.com&gt;
 * @since 0.1
 */
public class XPathParser
{
	/**
	 * Takes a node and applies an XPath expression to it.
	 * 
	 * @param node A <code>Node</code> object to use.
	 * @param xpathExpression The <code>String</code> containing an XPath expression.
	 * @return <code>String</code> containing the result, <code>null</code> if blank or an exception occurred.
	 */
	public static String parse(Node node, String xpathExpression)
	{
	    XPath xpath = XPathFactory.newInstance().newXPath();
	    String xpathEval = null;
	
	    try {
	        xpathEval = xpath.evaluate(xpathExpression, node);
	    } catch(XPathExpressionException e) {
	        return null;
	    }
	
	    return xpathEval;
	}
}